package user;
import book.Book;
import book.Library;
import book.PairOfUidAndBookId;
import constant.Constant;
import utils.AnalyzingBorrowedBook;
import utils.ScannerSingleton;
import java.io.IOException;
import java.util.Scanner;
import book.PairOfUidAndBookId;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 刘国水
 * Date: 2025-03-30
 * Time: 17:53
 */
public class NormalUser extends User{

//    public Scanner scanner = new Scanner(System.in);

    public Scanner scanner ;

    //存储图书借阅信息
    private PairOfUidAndBookId[] pairOfUidAndBookIds;
    //当前的借阅量
    private int borrowedCount;

    private final AnalyzingBorrowedBook analyzingBorrowedBook = new AnalyzingBorrowedBook();



    public NormalUser(String name,int userID) {
        super(name,userID,"普通用户");
        loadBorrowedBook();
        scanner = ScannerSingleton.getInstance();
    }

    private void loadBorrowedBook() {
        PairOfUidAndBookId[] allBorrowedBook;
        try {
            //1.先加载⽂件当中的借阅信息
            allBorrowedBook=
                    analyzingBorrowedBook.loadObject(Constant.BORROWED_BOOK_FILE_NAME);
            //2. 默认已借阅的图书数组⼤⼩为BORROW_BOOK_MAX_NUM，这⾥也可以定义到常量类
                    pairOfUidAndBookIds = new PairOfUidAndBookId[Constant.BORROW_BOOK_MAX_NUM];
                    //3.没有读取到已借阅的图书信息

            if (allBorrowedBook== null) {
                borrowedCount = 0;
            } else {
                //4. 查看实际读取到的数组⻓度是多少？
                int allBorrowedBookLen= allBorrowedBook.length;

                //5. 如果读取到了10本书被借阅 但是当前borrowedBooks数组⻓度⼩于10
                if (allBorrowedBookLen> pairOfUidAndBookIds.length) {
                 //6. 按照实际情况进⾏分配数组内存
                    pairOfUidAndBookIds = new PairOfUidAndBookId[allBorrowedBookLen];
                }

                 //7.把数据拷⻉回到 已借阅图书信息的数组当中
                for (int i = 0; i < allBorrowedBookLen; i++) {
                    pairOfUidAndBookIds[i] = allBorrowedBook[i];
                }

                //8.更新当前实际借阅书籍的书籍数量

                borrowedCount = allBorrowedBookLen;
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
    private void storeBorrowedBook() {
        try {
            analyzingBorrowedBook.storeObject(pairOfUidAndBookIds,
                    Constant.BORROWED_BOOK_FILE_NAME);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int display() {
        System.out.println("普通⽤⼾ " + this.getName() + " 的操作菜单:");
        System.out.println("1. 查找图书");
        System.out.println("2. 打印所有的图书");
        System.out.println("3. 退出系统");
        System.out.println("4. 借阅图书");
        System.out.println("5. 归还图书");
        System.out.println("6. 查看当前个⼈借阅情况");
        System.out.println("请选择你的操作：");
        return scanner.nextInt();
    }
    //借阅图书
    public void borrowBook() {
        scanner.nextLine();
        System.out.println("请输⼊你要借阅图书的id：");
        int bookId = scanner.nextInt();
        scanner.nextLine();
        //如果书架没有书 不能借阅
        Library library;
        if (library.getBookCount() == 0) {
            System.out.println("书架没有书籍暂且不能借阅");
            return;
        }
         //加载已借阅的图书信息
        loadBorrowedBook();
        //判断要借阅的书 是否存在
        Book book = library.searchById(bookId);
        if(book == null) {
            System.out.println("没有该id的相关书籍："+bookId);
            return;
        }
         //检查通过loadBorrowedBook()加载到pairOfUidAndBookIds数组当中
         //是否有bookId == 当前需要借阅的图书ID && UID也是⼀样的 说明当前⽤⼾借阅过
        //否则就是其他⽤⼾借阅过
        for (int i = 0; i < borrowedCount; i++) {

            PairOfUidAndBookId pairOfUidAndBookId = pairOfUidAndBookIds[i];
                //找到了对应的书籍
            if (pairOfUidAndBookId.getBookId() == book.getBookId()) {
                if (getUserID() == pairOfUidAndBookId.getUserId()) {
                    System.out.println("该书已经被你借阅过了，你的ID是：" + getUserID());
                    return;
                } else {
                    System.out.println("该书已经被其他⼈借阅过了，他的ID是：" +
                            pairOfUidAndBookId.getUserId());
                    return;
                }
            }
        }
        library.borrowBook(bookId);
        //封装对象写到 借阅表当中
        PairOfUidAndBookId pairOfUidAndBookId = new PairOfUidAndBookId(userID, book.getBookId());
        pairOfUidAndBookIds[borrowedCount] = pairOfUidAndBookId;
        borrowedCount++;
        //存储借阅图书
        storeBorrowedBook();
        System.out.println("借阅成功！");

    }

    //归还图书
    public void returnBook() {

    }
    // 查看个⼈借阅情况
    public void viewBorrowBooks() {

    }

}
